package setting

import (
	"github.com/gin-gonic/gin"
	"likeadmin/admin/schemas/req"
	"likeadmin/admin/service/setting"
	"likeadmin/core"
	"likeadmin/core/response"
	"likeadmin/middleware"
	"likeadmin/util"
)

var LoginSettingGroup = core.Group("/setting/login", newLoginSettingHandler, regLoginSetting, middleware.TokenAuth())

func newLoginSettingHandler(srv setting.ISettingLoginService) *loginHandler {
	return &loginHandler{srv: srv}
}

func regLoginSetting(rg *gin.RouterGroup, group *core.GroupBase) error {
	return group.Reg(func(handle *loginHandler) {
		rg.GET("/detail", handle.Detail)
		rg.POST("/save", handle.Save)
	})
}

type loginHandler struct {
	srv setting.ISettingLoginService
}

func (h loginHandler) Detail(c *gin.Context) {
	res, err := h.srv.Detail()
	response.CheckAndRespWithData(c, res, err)
}

func (h loginHandler) Save(c *gin.Context) {
	var loginReq req.SettingLoginReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &loginReq)) {
		return
	}
	response.CheckAndResp(c, h.srv.Save(loginReq))
}
